package com.wilson.integration.lease.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wilson.integration.lease.admin.service.*;
import com.wilson.integration.lease.common.entity.*;
import com.wilson.integration.lease.common.entity.vo.apartment.ApartmentDetailVO;
import com.wilson.integration.lease.common.entity.vo.apartment.ApartmentItemVO;
import com.wilson.integration.lease.common.entity.vo.apartment.ApartmentQueryVO;
import com.wilson.integration.lease.common.entity.vo.apartment.ApartmentSubmitVO;
import com.wilson.integration.lease.common.entity.vo.fee.FeeValueVO;
import com.wilson.integration.lease.common.entity.vo.graph.GraphVO;
import com.wilson.integration.lease.common.enums.ItemType;
import com.wilson.integration.lease.common.excption.LeaseException;
import com.wilson.integration.lease.common.mapper.*;
import com.wilson.integration.lease.common.result.ResultCodeEnum;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wilson
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {

    @Resource
    private GraphInfoService graphInfoService;

    @Resource
    private ApartmentFacilityService apartmentFacilityService;

    @Resource
    private ApartmentLabelService apartmentLabelService;

    @Resource
    private ApartmentFeeValueService apartmentFeeValueService;

    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;

    @Resource
    private GraphInfoMapper graphInfoMapper;

    @Resource
    private LabelInfoMapper labelInfoMapper;

    @Resource
    private FacilityInfoMapper facilityInfoMapper;

    @Resource
    private FeeValueMapper feeValueMapper;

    @Resource
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVO apartmentSubmitVO) {
        super.saveOrUpdate(apartmentSubmitVO);
        Long apartmentSubmitVOId = apartmentSubmitVO.getId();

        if (ObjectUtils.isNotEmpty(apartmentSubmitVOId)) {
            // 1.删除图片列表
            graphInfoService.remove(
                    new LambdaQueryWrapper<GraphInfo>()
                            .eq(GraphInfo::getItemType, ItemType.APARTMENT)
                            .eq(GraphInfo::getItemId, apartmentSubmitVOId));

            // 2.删除配套列表
            apartmentFacilityService.remove(
                    new LambdaQueryWrapper<ApartmentFacility>()
                            .eq(ApartmentFacility::getApartmentId, apartmentSubmitVOId));

            // 3.删除标签列表
            apartmentLabelService.remove(
                    new LambdaQueryWrapper<ApartmentLabel>()
                            .eq(ApartmentLabel::getApartmentId, apartmentSubmitVOId));

            // 4.删除杂费列表
            apartmentFeeValueService.remove(
                    new LambdaQueryWrapper<ApartmentFeeValue>()
                            .eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVOId));
        }

        // 1.插入图片列表
        List<GraphVO> graphVOList = apartmentSubmitVO.getGraphVOList();
        if (!CollectionUtils.isEmpty(graphVOList)) {
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVO graphVO : graphVOList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVOId);
                graphInfo.setName(graphVO.getName());
                graphInfo.setUrl(graphVO.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

        // 2.插入配套列表
        List<Long> facilityInfoIdList = apartmentSubmitVO.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIdList)) {
            ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
            for (Long facilityId : facilityInfoIdList) {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(apartmentSubmitVOId);
                apartmentFacility.setFacilityId(facilityId);
                facilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(facilityList);
        }

        // 3.插入标签列表
        List<Long> labelIds = apartmentSubmitVO.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVOId);
                apartmentLabel.setLabelId(labelId);
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }

        // 4.插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVO.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                apartmentFeeValue.setApartmentId(apartmentSubmitVOId);
                apartmentFeeValue.setFeeValueId(feeValueId);
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }
    }

    @Override
    public IPage<ApartmentItemVO> pageApartmentItemByQuery(IPage<ApartmentItemVO> page, ApartmentQueryVO queryVO) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page, queryVO);
    }

    @Override
    public ApartmentDetailVO getApartmentDetailById(Long id) {
        // 1.查询ApartmentInfo
        ApartmentInfo apartmentInfo = this.getById(id);
        if (ObjectUtil.isNull(apartmentInfo)) {
            return null;
        }

        // 2.查询GraphInfo
        List<GraphVO> graphVOList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);

        // 3.查询LabelInfo
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        // 4.查询FacilityInfo
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);

        // 5.查询FeeValue
        List<FeeValueVO> feeValueVOList = feeValueMapper.selectListByApartmentId(id);

        ApartmentDetailVO adminApartmentDetailVO = new ApartmentDetailVO();

        BeanUtils.copyProperties(apartmentInfo, adminApartmentDetailVO);
        adminApartmentDetailVO.setGraphVOList(graphVOList);
        adminApartmentDetailVO.setLabelInfoList(labelInfoList);
        adminApartmentDetailVO.setFacilityInfoList(facilityInfoList);
        adminApartmentDetailVO.setFeeValueVOList(feeValueVOList);

        return adminApartmentDetailVO;
    }

    @Override
    public void removeApartmentById(Long id) {
        Long count = roomInfoMapper.selectCount(
                new LambdaQueryWrapper<RoomInfo>()
                        .eq(RoomInfo::getApartmentId, id));
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        }

        // 1.删除GraphInfo
        graphInfoService.remove(
                new LambdaQueryWrapper<GraphInfo>()
                        .eq(GraphInfo::getItemType, ItemType.APARTMENT)
                        .eq(GraphInfo::getItemId, id));

        // 2.删除ApartmentLabel
        apartmentLabelService.remove(
                new LambdaQueryWrapper<ApartmentLabel>()
                        .eq(ApartmentLabel::getApartmentId, id));

        // 3.删除ApartmentFacility
        apartmentFacilityService.remove(
                new LambdaQueryWrapper<ApartmentFacility>()
                        .eq(ApartmentFacility::getApartmentId, id));

        // 4.删除ApartmentFeeValue
        apartmentFeeValueService.remove(
                new LambdaQueryWrapper<ApartmentFeeValue>()
                        .eq(ApartmentFeeValue::getApartmentId, id));

        // 5.删除ApartmentInfo
        super.removeById(id);
    }
}




